<?php
/**********************************************************************
 *                                                                    *    
 * Copyright (c) 2009 Dmytro Bezhnar. All rights reserved.     *
 *                                                                    *
 **********************************************************************/

/**
 * Ageset entity manager.
 *
 * @version $
 */
class TagsCommonManager extends ORMManager 
{	
    	
	function getPopularTags($project_id, $db , $args)
	{
		if ($args['limit']*1)
		{
			$limit = $args['limit']*1;
		}else 
		{
			$limit = 20;
		}
		
		$where = '';
		if ($args['city_id']*1) {
			$where .= ' and s.city_id='. $args['city_id']*1 .' '; 			
		}
		
		$qs = "SELECT 
						t.tag_id, t.name, t.click
					FROM 
						$db.news s, $db.news_tags st, $db.tags t 
					where 
						s.add_date >= DATE_SUB(CURDATE(),INTERVAL 7 DAY) and
						s.news_id=st.news_id and 
						st.tag_id=t.tag_id $where 
					group by t.tag_id 
					order by 
						t.click desc 
					limit $limit";
		return $this->getArray($qs, 60*25);
	}
	
	function getRandomTags($project_id, $db, $limit=20)
	{
		$qs = "SELECT * FROM $db.tags ORDER BY rand() limit $limit";
		return $this->getArray($qs, 60*3);
	}
	
	function getTagNameByTagId($tag_id, $project_id, $db)
	{
		$qs = "SELECT name FROM $db.tags where tag_id=$tag_id";
		$result = $this->getOneArray($qs);
		return $result['name'];
	}
	
	function getNewsListIdByTagId($tag_id, $args, $project_id, $db)
	{		
		$count_page=$args['count_page']*1;
		if (!$count_page)
		{
			$count_page=10;
		}
		$min = $args['page']*$count_page;
		
		$qs="SELECT 
					sql_calc_found_rows 
					news_id 
				FROM 
					$db.news_tags 
				WHERE 
					tag_id=$tag_id 
				ORDER BY 
					news_id desc 
				LIMIT 
					$min, $count_page";
		
		$data = $this->getArray($qs);
		#print $qs;
		$qs = "SELECT FOUND_ROWS() cnt";
		$cnt = $this->getOneArray($qs);
		
		return array('count' => $cnt['cnt'], 'data' => $data);
	}
	
	function getTagsByNewsId($news_id, $project_id, $db)
	{
		$qs="SELECT 
					t.* 
				FROM 
					$db.news_tags nt, 
					$db.tags t 
				WHERE 
					nt.news_id=$news_id and 
					nt.tag_id=t.tag_id";
		return $this->getArray($qs, 60*15);
	}
	
	function updateTagReitingByTagID($tag_id, $project_id, $db)
	{
		$qs = "UPDATE $db.tags SET click=click+1 WHERE tag_id=$tag_id";
		$this->execQuery($qs);
	}
	
	function updateTagReitingByNewsID($news_id, $project_id, $db)
	{
		$qs = "SELECT 
									tag_id 
								from 
									$db.news_tags 
								WHERE 
									news_id=$news_id";
		$tags_list = $this->getOneArray($qs, 60*15);
		
		$where = '(0 ';
		if ($tags_list)
		{
			foreach ($tags_list as $item)
			{
				$where .= ' or tag_id='.$item['tag_id'];
			}	
		}
		$where .= ') ';
		
		$qs = "UPDATE 
						$db.tags 
					SET 
						click=click+1 
					WHERE 
						$where";
		if ($tags_list)
		{
			$this->execQuery($qs, 60*15);
		}	
	}
}

?>